package com.project.controller.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.project.entity.User;
import com.project.service.StudentInfoService;
import com.project.service.UserService;
import com.project.service.base.BaseService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by TAO on 2015/10/4.
 */
@Controller
public class LoginController extends BaseService {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String loginForm(HttpServletRequest request,Model model) {
        //设置fastjson禁止检测循环引用
        JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();

        log.info("login>>>>>>>>>>>>>>");

        try {
            //用户是否登录
            Subject subject = SecurityUtils.getSubject(); // currentUser
            Session session=subject.getSession();

            //如果用户没有登录，跳转到登陆页面
            if (!subject.isAuthenticated()) {

                if (!userLogin(subject, request,session)) {//登录失败时跳转到登陆页面,userLogin() in BaseService

                    return  "userLogin";
                }
            }

            //执行到此，表示用户名、密码是正确的
            String username = request.getParameter("username");
            User user = userService.findByUserName(username);
            session.setAttribute("currentUser",user);//session中设置当前用户
            int userType = user.getUsertype();
            String path = "";
            //Todo 设置各个角色的信息表设置为session
            switch (userType)
            {
                case 1:
                    path = "/teacher/index";
                    break;
                case 2:
                    path ="student/studentIndex";
                    break;
                case 3:
                    path = "admin/adminIndex";
                    break;
                case 4:
                    path ="/enterprise/index";
                    break;
                case 5:
                    path = "/system/index";
                    break;
                case 6:
                    path = "/schoolAdmin/index";
                    break;
            }
            return path;
        }catch (Exception e)
        {
            e.printStackTrace();
            return  "userLogin";
        }


    }

    @RequestMapping(value = "/logout")
    public ModelAndView Logout(HttpServletRequest request){

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();

        if(subject.isAuthenticated()){
            session.stop();
            subject.logout();
            session = subject.getSession();
        }

        view.setViewName("/userLogin");
        return  view;
    }

}
